<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>时间格式化</title>
</head>
<body>
  <script>
    

    /* let addZero = val => {
      val = Number(val);
      return val < 10 ? '0' + val : val;
    };

    function formatTime(time) {
      // 1. 把时间字符串变为标准日期对象
      time = time.replace(/-/g, '/');
      time = new Date(time);
      // 2. 基于方法获取年月日等信息
      let year = time.getFullYear();
      let month = addZero(time.getMonth() + 1);
      let day = addZero(time.getDate());
      let hours = addZero(time.getHours());
      let minutes = addZero(time.getMinutes());
      let seconds = addZero(time.getSeconds());
      return year + '年' + month + '月' + day + '日' + ' ' + hours + ":" + minutes + ":" + seconds;
    }
    console.log(formatTime(time)); */

    /* 
    * 封装一套公共的时间字符串格式化处理的方式
    */
   String.prototype.formatTime = function formatTime(template) {
     // 初始化模板
     typeof template === 'undefined' ? template = "{0}年{1}月{2}日 {3}:{4}:{5}" : null;
    //  this: 我们要处理的字符串
    // 获取日期字符串中的数字
    let matchAry = this.match(/\d+/g);

    // 模板和数据的渲染(引擎机制)
    template = template.replace(/\{(\d+)\}/g, (x, y) => {
      let val = matchAry[y] || '00';
      val.length < 2 ? val = '0' + val : null;
      return val;
    });
    return template;
   };
   let time = '2021-1-23 12:0:0';
   console.log(time.formatTime());
  </script>
</body>
</html>